require(["config"],function(config){
	require(["jquery"],function(jq){
		//创建cookie
		function setCookie(name, value, iDay){
			var oDate=new Date();
			
			oDate.setDate(oDate.getDate()+iDay);
			
			document.cookie=name+'='+value+';expires='+oDate;
		}
		//获取cookie
		function getCookie(name)
		{
			var arr=document.cookie.split('; ');
			var i=0;
			for(i=0;i<arr.length;i++)
			{
				var arr2=arr[i].split('=');
				
				if(arr2[0]==name)
				{
					return arr2[1];
				}
			}
			
			return '';
		}
		//删除cookie
		function removeCookie(name){
			setCookie(name, '1', -1);
		}
		
		//登录
		window.onload = function(){
			//获取页面元素
			var user=document.getElementById("user")
			var paswd=document.getElementById("password")
			var btn=document.getElementById("register_btn")
			//给登录按钮加点击事件
			btn.onclick = function(){
				//获取用户输入的用户名和密码
				var usn = user.value;
				var pwd = paswd.value;
				
				//校验用户名和密码是否正确
				//获取到cookie中的用户信息
				var users = getCookie("test") ? getCookie("test") : "";
				//将字符串转为对象
				users = convertStrToObj(users);
				
				if(users[usn] == pwd){
					//登录成功
		//			setCookie("test",usn,7);
					console.log("登录成功!");
					window.location.href = "index.html";
				}else{
					//登录失败
					alert("用户名和密码不匹配，请确认后重试！");
				}
			};
			
		};
		//将字符串转为对象
		function convertStrToObj(str){
			if(!str){ //如果是空字符串
				return {}; //返回空对象
			}
			var users = str.split(":");
			var obj = {};
			for(var i = 0; i < users.length; i ++){
				var userData = users[i].split(",");
				obj[userData[0]] = userData[1];
			}
			return obj;
		}
		
		//将对象转为字符串
		function convertObjToStr(obj){
			var str = "";
			//遍历对象
			for(var usn in obj){
				var pwd = obj[usn];
				if(str){
					str += ":";
				}
				str += usn + ',' + pwd;
			}
			return str;
		}
	});
});

